package com.flrjcx.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.flrjcx.common.constants.HttpStateConstants;
import com.flrjcx.common.constants.MessageConstants;
import com.flrjcx.common.utils.HttpPoolUtils;
import com.flrjcx.common.utils.ValidateCodeUtils;
import com.flrjcx.dao.LoginDao;
import com.flrjcx.common.enums.MessageHintEnum;
import com.flrjcx.common.enums.ValidStatusEnum;
import com.flrjcx.common.model.UserModel;
import com.flrjcx.common.model.result.ResponseData;
import com.flrjcx.model.TplSingleSend;
import com.flrjcx.service.LoginService;
import com.flrjcx.common.utils.EncryptionUtils;
import com.flrjcx.common.utils.RedisCache;
import com.yunpian.sdk.YunpianClient;
import com.yunpian.sdk.model.Result;
import com.yunpian.sdk.model.SmsSingleSend;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 注册服务
 *
 * @author Flrjcx
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class LoginServiceImpl implements LoginService {

    @Resource
    private LoginDao loginDao;
    @Resource
    private RedisCache redisCache;
    private String globalPassName;
    private String globalPassPassword;

       /**
     * 云片网API_KEY
     */
    private static final String API_KEY = "4f8043ca22689e9c36f30ac406ff763a";
    /**
     * 模板id
     */
    private static final long TPL_ID = 2228042;
    /**
     * 验证码短信内容模板
     */
    private static final String CAPTCHA_TEXT_TEMPLATE = "您的验证码是"+ValidateCodeUtils.generateValidateCode(6)+"。如非本人操作，请忽略本短信！";

    @Override
    public ResponseData verifyLoginBeforeAll(UserModel userModel) {
        String name = userModel.getName();
        String password = userModel.getPassword();
        /** 账号为空 */
        if (null == name || MessageHintEnum.StrIsNull.getMessage().equals(name)) {
            return ResponseData.buildErrorResponse(HttpStateConstants.HTTP_STATE_402, MessageConstants.VERIFY_NAME_NULL);
        }
        /** 密码为空 */
        if (null == password || MessageHintEnum.StrIsNull.getMessage().equals(password)) {
            return ResponseData.buildErrorResponse(HttpStateConstants.HTTP_STATE_402, MessageConstants.VERIFY_PASSWORD_NULL);
        }
        /** 验证redis中是否有该用户 */
        if (redisCache.verifyKey(name)) {
            String passName = redisCache.getKey(name);
            String md5 = EncryptionUtils.md5(password);
            String passPassword = redisCache.getCacheObject(passName);
            /** 将用户输入的密码和redis中存储的密码对比 */
            if (md5.equals(passPassword)) {
                return ResponseData.buildResponse(MessageConstants.VERIFY_PASS, HttpStateConstants.HTTP_STATE_200);
            } else {
                return ResponseData.buildErrorResponse(HttpStateConstants.HTTP_STATE_402, MessageConstants.VERIFY_PASS_ERROR);
            }
        }
        /** 检查是邮箱登录还是用户名登录*/
        if (name.indexOf(MessageHintEnum.SpecialStr.getMessage()) != ValidStatusEnum.MinusStatus.getCode()) {
            /** 邮箱登录 */
            String passVerifyEmail = loginDao.verifyEmail(name);
            if (name.equals(passVerifyEmail)) {
                String md5 = EncryptionUtils.md5(password);
                String passPassword = loginDao.verifyEmailPassword(md5, passVerifyEmail);
                globalPassName = passVerifyEmail;
                globalPassPassword = passPassword;
                if (!md5.equals(passPassword)) {
                    return ResponseData.buildErrorResponse(HttpStateConstants.HTTP_STATE_402, MessageConstants.VERIFY_PASS_ERROR);
                }
            } else {
                return ResponseData.buildErrorResponse(HttpStateConstants.HTTP_STATE_402, MessageConstants.VERIFY_PASS_ERROR);
            }
        } else {
            /** 用户名登录 */
            String passName = loginDao.verifyName(name);
            if (name.equals(passName)) {
                String md5 = EncryptionUtils.md5(password);
                String passPassword = loginDao.verifyPassword(md5, passName);
                if (md5.equals(passPassword)) {
                    globalPassName = passName;
                    globalPassPassword = passPassword;
                    redisCache.loginSaveRedis(passName,passPassword);
                    return ResponseData.buildResponse(MessageConstants.VERIFY_PASS, HttpStateConstants.HTTP_STATE_200);
                } else {
                    return ResponseData.buildErrorResponse(HttpStateConstants.HTTP_STATE_402, MessageConstants.VERIFY_PASS_ERROR);
                }
            } else {
                return ResponseData.buildErrorResponse(HttpStateConstants.HTTP_STATE_402, MessageConstants.VERIFY_PASS_ERROR);
            }
        }
        redisCache.loginSaveRedis(globalPassName,globalPassPassword);
        return ResponseData.buildResponse(MessageConstants.VERIFY_PASS, HttpStateConstants.HTTP_STATE_200);
    }

    @Override
    public List<Map> testMyBatis() {
        return loginDao.testMyBatis();
    }

    @Override
    public String tplSendTest(String phone) throws UnsupportedEncodingException {

        return null;
    }
}
